package com.hnust.dao;

import com.hnust.domain.Teacher;
import com.hnust.domain.record.SearchTeacherInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *创建人：张汝往
 *创建时间：2020/10/6
 *描述：
 */
@Mapper
@Repository
public interface TeacherDao {

    //根据学院ID 查找老师总数
    @Select("select count(*) from teacher where col_id=#{collegeId}")
    @ResultType(Integer.class)
    public Integer findTeacherCountByCollegeId(String collegeId);

    //根据条件查询所有的老师
    @SelectProvider(type = findTeacherBySearchProvider.class,method = "findStudentBySearchSql")
    @ResultType(Teacher.class)
    public List<Teacher> findTeacherBySearch(String collegeId, SearchTeacherInfo searchTeacherInfo);
    class findTeacherBySearchProvider{
        public String findStudentBySearchSql(String collegeId, SearchTeacherInfo searchTeacherInfo){
            String sql="select * from teacher where 1=1 ";
            if(searchTeacherInfo!=null){
                if(searchTeacherInfo.getAccount()!=null){
                    sql=sql+" and tea_number=#{searchTeacherInfo.account}";
                }
                if(searchTeacherInfo.getName()!=null){
                    sql=sql+" and name like concat('%',#{searchTeacherInfo.name},'%')";
                }
            }
            sql=sql+" and col_id=#{collegeId}";
            return sql;
        }
    }

    //根据老师ID查询所有的老师
    @Select("select * from teacher where tea_id=#{teacherId}")
    @ResultType(Teacher.class)
    public Teacher findTeacherById(String teacherId);

    //根据老师Id删除老师
    @Delete("delete from teacher where tea_id=#{teacherId}")
    @ResultType(Boolean.class)
    Integer deleteTeacherByID(String teacherId);


    //根据老师ID更新老师信息
    @Update("update teacher set tea_number=#{teaNumber},name=#{name},password=#{password}" +
            ",pho_number=#{phoNumber},e_mail=#{eMail} where tea_id=#{teaId}")
    @ResultType(Boolean.class)
    public Boolean updateTeacher(Teacher teacher);


    @Select("select * from teacher where col_id=#{collegeId}")
    @ResultType(Teacher.class)
    public List<Teacher> getTeacherByCollegeId(String collegeId);

    // 插入一个老师
    @Insert("insert into teacher (tea_id, tea_number, name, password, pho_number, e_mail, col_id) value (#{tea_id}, #{tea_number}, #{name}, #{password}, #{pho_number}, #{e_mail}, #{col_id})")
    @ResultType(Integer.class)
    Integer insertTeacher(String tea_id, String tea_number, String name, String password, String pho_number, String e_mail, String col_id);
}
